package com.gym.emp.web.controller;

import com.gym.emp.common.Const;
import com.gym.emp.entity.Emp;
import com.gym.emp.services.EmpServices;
import com.gym.emp.util.PhoneCodeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @author ming
 * 2021/11/19
 * description:
 */
@Controller
@RequestMapping("/sys")
public class SystemController {

    @Autowired
    private EmpServices es;

    @RequestMapping("/getCode")
    @ResponseBody
    public String getCode(String phone, HttpSession session) {
        Emp emp = es.getEmpByPhone(phone);
        if (emp != null) {
            //调用发送手机短信的工具类
            String phoneCode = PhoneCodeUtil.getCode(phone);
//            System.out.println(phone + ":" + phoneCode);
            //把手机验证码存到session当中
            session.setAttribute(Const.PHONE_CODE, phoneCode);
            return Const.SUCCESS;
        } else {
//            return "{\"status\":1,\"msg\":\"手机号码不存在\"}";
            return Const.FAILE;
        }
    }
    @RequestMapping("/checkCode")
    @ResponseBody
    public String checkCode(String code, HttpSession session) {
        if (StringUtils.isNotEmpty(code)) {
            String pc = (String) session.getAttribute(Const.PHONE_CODE);
            System.out.println("pc:"+pc);
            System.out.println("code:"+code);
            if (code.equals(pc)) {
                return Const.SUCCESS;
            }
        }
        return Const.FAILE;
    }
}
